package org.mspring.mlog.task.ping;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.mspring.mlog.entity.News;
import org.mspring.mlog.entity.Post;
import org.mspring.mlog.task.ping.task.NewsPingTask;
import org.mspring.mlog.task.ping.task.PostPingTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月25日
 */
@Aspect
@Component
public class PingAspect {
    private static final Logger log = Logger.getLogger(PingAspect.class);

    @Autowired
    private NewsPingTask newsPingTask;

    @Autowired
    private PostPingTask postPingTask;

    @AfterReturning(value = "execution(* org.mspring.mlog.service.impl.NewsServiceImpl.save(..))", returning = "news")
    public void newsPingTask(JoinPoint jp, News news) {
        try {
            Map<Object, Object> context = new HashMap<Object, Object>();
            context.put("id", news.getId());
            newsPingTask.doAsyncTask(context);
        } catch (Exception e) {
            // TODO: handle exception
            log.debug("ping error, newsId = " + news.getId());
        }
    }

    @AfterReturning(value = "execution(* org.mspring.mlog.service.impl.PostServiceImpl.save(..))", returning = "post")
    public void postPingTask(JoinPoint jp, Post post) {
        try {
            Map<Object, Object> context = new HashMap<Object, Object>();
            context.put("post", post);
            postPingTask.doAsyncTask(context);
        } catch (Exception e) {
            // TODO: handle exception
            log.debug("ping error, newsId = " + post.getId());
        }
    }
}
